set more off

clear matrix
clear mata
clear
set mem 10000m
set maxvar 32767
set matsize 11000

capture log close
log using "$output\mergewithflorida.log", replace

cd "C:\Florida projects\dissertation\"

use longFldatavariables.dta

sort id year
rename id unique_random_id
merge 1:1 unique_random_id year using "F:\Florida projects\dissertation\school grades pollution\attendance_behavior_deidentified.dta", update gen(_mabsent)
drop if unique_random_id==""
drop if school==.

rename unique_random_id id
sort school year

*Merging in the school level vital statistics data on maternal education and marital status
merge m:1 school year using longavgbyschool.dta, gen(_m2)

*Merging in the matched school-wind-highways data
merge m:1 school using "C:\Users\Claudia Persico\Dropbox\Research on Florida Wind Patters\School wind pollution\schools downwind\presubmission archive\collapsed_All0629.dta", gen(_mrd)

drop if id==""
drop if school==.

merge m:1 school year using "C:\Users\Claudia Persico\Dropbox\Research on Florida Wind Patters\roadsetup\samples\remake\collapsed_allyrstestweek2010.dta", gen(_mrdtest)
drop if id==""

*Cleaning road variables
sort school
by school: egen mi_to_nid_aadt18_4=max(mi_to_nid_aadt18)
by school: egen mi_to_nid_aadt25_4=max(mi_to_nid_aadt25)
by school: egen mi_to_nid_aadt48_4=max(mi_to_nid_aadt48)
by school: egen mi_to_nid_IS4=max(mi_to_nid_IS)
by school: egen mi_to_nid_USH4=max(mi_to_nid_USH)

*Generating race variables
gen white = (racial_ethnic_cd =="W")
gen black=  (racial_ethnic_cd =="B")	
gen hisp=  (racial_ethnic_cd =="H")	

*Merging in NCES data on schools
merge m:1 school year using "C:\Users\Claudia Persico\Dropbox\Research on Florida Wind Patters\School wind pollution\ncesfsir.dta", update gen(_m4)

drop if id==""
sort id year

*Merging in and cleaning FSIR data on schools
merge m:1 school year using "C:\Users\Claudia Persico\Dropbox\Research on Florida Wind Patters\School wind pollution\fsircensusschoolfixed.dta", update gen(_m5)
drop if id==""

destring DistrictNumber, force replace 
destring SchoolNumber, force replace
destring SchoolType, force replace
rename school uniqueID
merge m:1 uniqueID year using "C:\Users\Claudia Persico\Dropbox\Research on Florida Wind Patters\School wind pollution\comboschoolcovars.dta", update gen(_covupd) 
rename uniqueID school
drop if id==""

*merge m:1 school using "C:\Florida Projects\dissertation\schools wind pollution\samples\newdataintensity2010.dta", gen(_oldnew)

sort school year
foreach i in TeachDegree size stability FRL {
replace `i'2=`i' if `i'2==.
}
foreach i in TeachDegree size stability FRL {
drop `i'
rename `i'2 `i'
}

*Making grade variables and fixed effects
g grade1=grade
destring grade1, force replace
replace grade1=. if grade1==1 | grade1==2
replace grade1=. if grade1==30 | grade1==31
tab grade1, gen(gradedum)
rename Grade schoolgrade

*Making student fixed effects
sort id year
egen studentef = group(id)

*Imputing zip codes to missing years 
sort school year
by school: egen zip=max(lzip)

sort id year

rename id studentid
rename studentef id
order studentid school year

save "C:\Florida projects\dissertation\schools wind pollution\samples\FLschoolswindroadsid2010ncesfsirnewaadt4mi18k112118.dta", replace
